package com.polaris.live.floating

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.WindowFloatingBinding
import com.polaris.live.utils.image.ImageUtils
import org.libpag.PAGScaleMode

/**
 * FloatingView
 *
 * @author Created by 天晴 on 2024/11/20 10:22
 * @since 1.0.0
 **/
class FloatingView(context: Context, attributeSet: AttributeSet?) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WindowFloatingBinding.inflate(LayoutInflater.from(context), this)

    private var mClickListener: (() -> Unit)? = null

    var mType = 0  //0正常  1左缩小  2 右缩小

    init {
        mBinding.closeBtn.setOnSingleClickListener {
            mClickListener?.invoke()
        }
        mBinding.pagView.apply {
            path = PagConst.VOICE_FLUCTUATIONS
            setRepeatCount(-1)
            setScaleMode(PAGScaleMode.Zoom)
        }
        setType(mType, true)
    }

    fun clickListener(back: () -> Unit) {
        mClickListener = back
    }

    fun setImage(url: String?) {
        url?.let {
            Glide.with(context)
                .load(ImageUtils.imgPath(it))
                .apply(RequestOptions.circleCropTransform())
                .into(mBinding.userImg)
        }
    }

    fun setType(type: Int, inner: Boolean = false) {
        if (!isAttachedToWindow && !inner) {
            return
        }
        mType = type
        when (type) {
            0 -> {
                mBinding.floatingWelt.visibility = GONE
                mBinding.closeBtn.visibility = VISIBLE
                mBinding.dataFl.visibility = VISIBLE
                mBinding.pagView.play()
            }

            1 -> {
                val lp = mBinding.floatingWelt.layoutParams as ConstraintLayout.LayoutParams
                lp.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
                lp.endToEnd = ConstraintLayout.LayoutParams.UNSET
                mBinding.floatingWelt.layoutParams = lp

                mBinding.floatingWelt.scaleX = -1f
                mBinding.floatingWelt.visibility = VISIBLE
                mBinding.closeBtn.visibility = GONE
                mBinding.dataFl.visibility = GONE
                mBinding.pagView.pause()
            }

            2 -> {
                val lp = mBinding.floatingWelt.layoutParams as ConstraintLayout.LayoutParams
                lp.startToStart = ConstraintLayout.LayoutParams.UNSET
                lp.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
                mBinding.floatingWelt.layoutParams = lp

                mBinding.floatingWelt.scaleX = 1f
                mBinding.floatingWelt.visibility = VISIBLE
                mBinding.closeBtn.visibility = GONE
                mBinding.dataFl.visibility = GONE
                mBinding.pagView.pause()
            }
        }
    }
}